Results 1 to 13 of 13

Thread: clear()..in a QGraphicsScene..

  1. #1
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Exclamation clear()..in a QGraphicsScene..

    Hi all
    I am using a clear() method in one of the clases inherited from QGraphicsScene ,it compiles succesfully and runs once quite succefully but when i give the inputs again and want a redraw by using clear function the application hangs ..below is the excerpt of the code..

    Qt Code:
    1. QWIDGET CLASS
    2. void InputItems::gridParam()
    3. {
    4. qDebug() << "Grid Parameter ";
    5. bool ok;
    6. myView->setParam(heightValue->text().toDouble(&ok), widthValue->text().toDouble(&ok));
    7. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. VIEW CLASS
    2. void View::setParam(double heightValue,double widthValue)
    3. {
    4. myScene->drawScene(heightValue,widthValue);
    5. qDebug() << "View Height ";
    6.  
    7. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. SCENE CLASS
    2. ..
    3. QVector <QGraphicsRectItem*> rectItem;
    4. ..
    5. void Scene::drawScene(double heightValue, double widthValue)
    6. {
    7. this->heightValue = heightValue;
    8. this->widthValue = widthValue;
    9. clear();
    10. drawMatrix(); //uses and draws a rect items
    11. qDebug() << "Scene Height " << heightValue ;
    12. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: clear()..in a QGraphicsScene..

    Be careful.
    The documentation says this when using clear()
    Removes and deletes all items from the scene
    Note that clear() also deletes your items on the scene.

    I guess you do not create a new rectItem again when updaing the scene. Thus, you try to access an inexisting item which leads to a crash.

  3. #3
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: clear()..in a QGraphicsScene..

    absolutelyt right...i understand that...thanks...
    now i am creating my item in the drawMatrix function instead of declaring it as a class variable....but then it wont be visible anymore out of the function ,what else can i do here..??

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: clear()..in a QGraphicsScene..

    What does drawMatrix() look like?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: clear()..in a QGraphicsScene..

    Qt Code:
    1. void Scene::drawMatrix()
    2. {
    3. QVector <QGraphicsRectItem*> rectItem; // i want this to be as a global variable /class variable so it is visible everywhere in the class...
    4.  
    5. qDebug() << "Scene Matrix ";
    6.  
    7. qreal rectWidth = (300 / widthValue) - 5;
    8.  
    9. qreal rectHeight = (300 / heightValue) - 5;
    10.  
    11. qreal startX = 1;
    12.  
    13. qreal startY = 1;
    14.  
    15.  
    16. int counterX = 0;
    17.  
    18. int counterY = 0;
    19.  
    20. int number = 0;
    21.  
    22. for(counterY = 0; counterY < heightValue; ++counterY) {
    23.  
    24. startY = counterY * (rectHeight + 5);
    25.  
    26. for(counterX = 0; counterX < widthValue; ++counterX) {
    27.  
    28. startX = counterX * (rectWidth + 5);
    29.  
    30. rectItem << addRect(0,0,rectWidth,rectHeight,QPen(),Qt::NoBrush);
    31.  
    32. qDebug() << counterX << counterY ;
    33.  
    34. rectItem.at(number)->setPos(startX,startY);
    35.  
    36. ++number;
    37.  
    38. }
    39.  
    40. }
    To copy to clipboard, switch view to plain text mode 

    too add to this piece of code ,i tried using QVector <QGraphicsRectItem *> *rectItem instead of QVector <QGraphicsRectItem*> rectItem ...so that i can declare it new after every clear() statement ,but it wont help ,even i am not sure that is this appropiate logically but i just tried to tried...

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: clear()..in a QGraphicsScene..

    You don't have to delete and recreate all the items each time you change the matrix. The problem you have is probably caused by the line #34. When you clear the scene, items are deleted but they are not removed from your global vector. It's enough that you clear the vector as well and your code should work. I would change the #34 line to use the result of addRect() instead of getting the item from the vector each time. In other words your code can be simplified and clarified and I think you should focus on that.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: clear()..in a QGraphicsScene..

    You don't have to delete and recreate all the items each time you change the matrix.
    Why not ?.....what difference does it makes if i use scene.clear() ...or maybe clear the vector...
    when you clear the scene, items are deleted but they are not removed from your global vector. It's enough that you clear the vector as well and your code should work
    do you mean i shouldnt use scene.clear and just clear the vector and at next user input the new matrix will be redrawn..?
    since u r master .. i will ask u one more thing ,is it logically correct ,as ahead i have to see if there are any mouse button pushes over a particular rectangle and then do some stuff for it...i mean thats the reason i am using a vector so i can follow each of the rectArea...

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: clear()..in a QGraphicsScene..

    Quote Originally Posted by salmanmanekia View Post
    Why not ?
    Because recreating the same elements over and over again doesn't make sense and it takes time.

    .....what difference does it makes if i use scene.clear() ...or maybe clear the vector...
    It makes great difference. Clearing the scene deletes items kept in the scene. Clearing the vector doesn't. That's why you need both if you want to have your vector.


    is it logically correct ,as ahead i have to see if there are any mouse button pushes over a particular rectangle and then do some stuff for it...i mean thats the reason i am using a vector so i can follow each of the rectArea...
    I don't understand your question.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: clear()..in a QGraphicsScene..

    I don't understand your question.
    he question is
    "i have decided to use QVector of QGraphicsRectItem because i have to make arbitrary numbers of rectangle and want to see when a user pushes a mouse button on any of the specifice button ,so i can check when and if a mouse event is trigred on a specific rectangle"does this makes it any clear...
    It makes great difference. Clearing the scene deletes items kept in the scene. Clearing the vector doesn't. That's why you need both if you want to have your vector.
    You don't have to delete and recreate all the items each time you change the matrix
    The above two lines confuses me should i delete the items and the vector both
    and last but not the least
    I would change the #34 line to use the result of addRect() instead of getting the item from the vector each time.
    how would you do that and by doing it will my main problem of having the the declaration of QVector<QGraphicsRectItem *> will be outside the member function

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: clear()..in a QGraphicsScene..

    Quote Originally Posted by salmanmanekia View Post
    he question is
    "i have decided to use QVector of QGraphicsRectItem because i have to make arbitrary numbers of rectangle and want to see when a user pushes a mouse button on any of the specifice button ,so i can check when and if a mouse event is trigred on a specific rectangle"does this makes it any clear...
    I still don't understand You want to know which rect item the mouse was pressed on or...what buttons are you talking about?


    The above two lines confuses me should i delete the items and the vector both
    Clearing the vector doesn't delete the items. So if you have N items, clear the scene, then add (N) items to the vector again without clearing it first will cause your vector to have 2*N items with first N of them being invalid so when you use QVector::at() to access item at position < N then you are accessing an invalid item causing your program to crash. For a local vector it works, because at each time you create a new vector but for a global vector the situation is as I described it.

    how would you do that and by doing it will my main problem of having the the declaration of QVector<QGraphicsRectItem *> will be outside the member function
    addRect() returns a pointer to the item which you are immediately adding to the vector without storing it anywhere. Then you reach into the vector to get the pointer back and set the position of the item. Instead you can assign the pointer to a local variable first and only then add the item to the vector so that you don't have to immediately query it back causing a segfault as I described earlier.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: clear()..in a QGraphicsScene..

    addRect() returns a pointer to the item which you are immediately adding to the vector without storing it anywhere. Then you reach into the vector to get the pointer back and set the position of the item. Instead you can assign the pointer to a local variable first and only then add the item to the vector so that you don't have to immediately query it back causing a segfault as I described earlier.
    Does that means that in this way the clear wont delete the items from the scene which are stored in QVector and only the locally declared pointer of QGraphicsRectItem will be cleaned..Right ..??..


    I still don't understand You want to know which rect item the mouse was pressed
    Right ,that what i am doing all this hardwork for,.....
    Last edited by salmanmanekia; 13th June 2010 at 18:19.

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: clear()..in a QGraphicsScene..

    Quote Originally Posted by salmanmanekia View Post
    Does that means that in this way the clear wont delete the items from the scene which are stored in QVector and only the locally declared pointer of QGraphicsRectItem will be cleaned..Right ..??..
    No. I'm sorry, I won't give you a lecture here on how pointers work.

    Right ,that what i am doing all this hardwork for,.....
    Subclass the rect item class and reimplement its events. Then you will know which item was clicked. Alternatively just query the scene about the item that is in the position where the event happened with QGraphicsScene::items().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Jan 2008
    Location
    Finland /Pakistan
    Posts
    216
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: clear()..in a QGraphicsScene..

    No. I'm sorry.
    Thats Ok ,i have an old enmity against pointers ...Still this wont stop me asking a question ,

    As per your advice i changed the code and it works perfectly and the change is as follow

    Qt Code:
    1. localRectItem = addRect(0,0,rectWidth,rectHeight,QPen(),Qt::NoBrush);
    2. localRectItem->setPos(startX,startY);
    3. rectItem.append(localRectItem);
    To copy to clipboard, switch view to plain text mode 

    while the previous not-so-good code was
    Qt Code:
    1. rectItem << addRect(0,0,rectWidth,rectHeight,QPen(),Qt::NoBrush);
    2. rectItem.at(number)->setPos(startX,startY);
    To copy to clipboard, switch view to plain text mode 

    What makes these two pieces of code so different that if i use the second code then i cant declare rectItem as a class variable ...i cant absorb this fact...
    I won't give you a lecture here on how pointers work
    May be one line answer for that or few line detailed answer is good too..No lectures please.!....

Similar Threads

  1. How can i clear the rawData?
    By Vincenzo in forum Qwt
    Replies: 1
    Last Post: 15th March 2009, 11:45
  2. how can i clear a file?
    By Vincenzo in forum Newbie
    Replies: 2
    Last Post: 18th January 2009, 11:05
  3. Replies: 10
    Last Post: 11th August 2008, 21:47
  4. Qt clear fonts
    By bunjee in forum Qt Programming
    Replies: 1
    Last Post: 2nd July 2008, 07:45
  5. Clear a Scene
    By peace_comp in forum Qt Programming
    Replies: 7
    Last Post: 20th May 2008, 18:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.